﻿using ProjectScheduling.daos;
using ProjectScheduling.models;

namespace ProjectScheduling
{
    public partial class AddRestDayForm : Form
    {
        public static AddRestDayForm b = new();
        public Action? save;
        internal void InitData(RestDay day)
        {
            Id = day.Id;
            if (day.Day > DateTime.MinValue)
            {
                txt_day.Value = day.Day;
                //txt_day.Text = day.Day.ToString("yyyy-MM-dd");
            }
            else
            {
                txt_day.Text = "";
            }

            txt_disc.Text = day.Disc;
            cb_rest.Checked = day.IsRest;
        }
        public AddRestDayForm()
        {
            InitializeComponent();
        }
        private int Id { get; set; }
        private void btn_save_Click(object sender, EventArgs e)
        {
            var data = new RestDay
            {
                Day = DateTime.Parse(txt_day.Text),
                IsRest = cb_rest.Checked,
                Disc = txt_disc.Text
            };
            if (data.Day.DayOfWeek == DayOfWeek.Sunday || data.Day.DayOfWeek == DayOfWeek.Saturday)
            {
                if (data.IsRest)
                {
                    MessageBox.Show("周末不能设置为休息日");
                    return;
                }
            }
            else
            {
                if (!data.IsRest)
                {
                    MessageBox.Show("除了周末的日期只能设置为休息日");
                    return;
                }
            }
            if (RestDayDao.Base.Exists(it => it.Day == data.Day && it.Id != Id))
            {
                MessageBox.Show("当前日期已经设置了，不能重复设置");
                return;
            }


            if (Id == 0)
            {
                data.CreateTime = DateTime.Now;
                RestDayDao.Base.Add(data);
            }
            else
            {
                data.Id = Id;
                RestDayDao.Base.Update(data, it => new { it.Day, it.IsRest, it.Disc });
            }
            MessageBox.Show("保存成功");
            save?.Invoke();
            Close();
        }
    }
}
